import {defineStore} from 'pinia';
import Cookies from 'js-cookie';
import {Session} from '/@/utils/storage';
import {useLoginApi} from '/@/api/login';
import msg from '/@/utils/messageUtils';

/**
 * 用户信息
 * @methods setUserInfos 设置用户信息
 */
export const useUserInfo = defineStore('userInfo', {
    state: (): UserInfosState => ({
        userInfos: {
            userName: '',
            photo: '',
            time: 0,
            roles: [],
            authBtnList: [],
        },
    }),
    actions: {
        async setUserInfos() {
            try {
                const {data} = await useLoginApi().userInfo();
                this.userInfos.userName = data.username;
                this.userInfos.photo = data.avatar;
                this.setUserBtns(data.btns);
            } catch (error) {
                msg().error('请求接口异常，请重试');
            }
        },

        setPhoto(avatarUrl: string) {
            this.userInfos.photo = avatarUrl;
        },
        setUserBtns(btns: Array<string>) {
            console.log('set user btns: ', JSON.stringify(btns));
            this.userInfos.authBtnList = btns;
        },

        // 模拟接口数据
        // https://gitee.com/lyt-top/vue-next-admin/issues/I5F1HP
        async getApiUserInfo() {
            return new Promise((resolve) => {
                setTimeout(() => {
                    // 模拟数据，请求接口时，记得删除多余代码及对应依赖的引入
                    const userName = Cookies.get('userName');
                    // 模拟数据
                    let defaultRoles: Array<string> = [];
                    let defaultAuthBtnList: Array<string> = [];
                    // admin 页面权限标识，对应路由 meta.roles，用于控制路由的显示/隐藏
                    let adminRoles: Array<string> = ['admin'];
                    // admin 按钮权限标识
                    let adminAuthBtnList: Array<string> = ['btn.add', 'btn.del', 'btn.edit', 'btn.link'];
                    // test 页面权限标识，对应路由 meta.roles，用于控制路由的显示/隐藏
                    let testRoles: Array<string> = ['common'];
                    // test 按钮权限标识
                    let testAuthBtnList: Array<string> = ['btn.add', 'btn.link'];
                    // 不同用户模拟不同的用户权限
                    if (userName === 'admin') {
                        defaultRoles = adminRoles;
                        defaultAuthBtnList = adminAuthBtnList;
                    } else {
                        defaultRoles = testRoles;
                        defaultAuthBtnList = testAuthBtnList;
                    }
                    // 用户信息模拟数据
                    const userInfos = {
                        userName: userName,
                        photo: "",
                        time: new Date().getTime(),
                        roles: defaultRoles,
                        authBtnList: defaultAuthBtnList,
                    };
                    Session.set('userInfo', userInfos);
                    resolve(userInfos);
                }, 0);
            });
        },
    },
});
